package cn.wolfcode.rbac.service.impl;

import cn.wolfcode.rbac.domain.User6;
import cn.wolfcode.rbac.mapper.User6Mapper;
import cn.wolfcode.rbac.query.PageResult;
import cn.wolfcode.rbac.query.QueryObject;
import cn.wolfcode.rbac.service.IUser6Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class User6ServiceImpl implements IUser6Service {

    @Autowired
    private User6Mapper user6Mapper;

    @Override
    public void saveOrUpdate(User6 user6) {
        if (user6.getId()!=null){
            // id不为空,说明此时调用的是修改操作
            // Update 表名 set name=?,sn=? where id=?;
            user6Mapper.updateByPrimaryKey(user6);
        }else {
            // id为空,说明此时调用的是保存操作
            // insert into 表名(name,sn) values(?,?)
            user6Mapper.insert(user6);
        }
    }

    @Override
    public void delete(Long id) {
        user6Mapper.deleteByPrimaryKey(Math.toIntExact(id));
    }

    @Override
    public User6 getOne(Long id) {
        User6 user6 = user6Mapper.selectByPrimaryKey(Math.toIntExact(id));
        return user6;
    }

    @Override
    public List<User6> listAll() {
        List<User6> user6s = user6Mapper.selectAll();
        return user6s;
    }

    @Override
    public PageResult query(QueryObject qo) {
        //查询总条数
        int totalCount = user6Mapper.queryForCount();
        //查询带有分页效果的结果集
        if (totalCount == 0){
            //查不出数据,调用的是pageResult类中的缺参构造器
            return new PageResult(qo.getCurrentPage(),qo.getPageSize());
        }

        List<User6> data = user6Mapper.queryForList(qo);
        return new PageResult(data,totalCount,qo.getCurrentPage(),qo.getPageSize());
    }
}
